<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <p>
        <button>赞(<span>0</span>)</button>
        <button>赞(<span>0</span>)</button>
        <button>赞(<span>0</span>)</button>
        <button>赞(<span>0</span>)</button>
    </p>
</body>
<script>
    // 获取元素
    const btnS = document.querySelectorAll('button');

    // btnS.forEach((el, i) => el.onclick = () => el.querySelector('span').textContent = el.querySelector('span').textContent * 1 + 1);

    // for (let i = 0; i < btnS.length; i++) {
    //     btnS[i].onclick = function() {
    //         this.querySelector('span').textContent = this.querySelector('span').textContent * 1 + 1;
    //     }
    // }

    // 使用闭包延长 var 变量 i 的作用范围
    for (var i = 0; i < btnS.length; i++) {
        (function(i) {
            btnS[i].onclick = function() {
                this.querySelector('span').textContent = this.querySelector('span').textContent * 1 + 1;
            }
        })(i);
    }
</script>

</html>